home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Cache / Application.php next >
PHP Script  |  2004-10-01  |  6KB  |  178 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PEAR :: Cache                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Richard Heyes <richard@phpguru.org>                         |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Application.php,v 1.5 2003/01/04 11:54:45 mj Exp $
  20.  
  21. require_once 'Cache.php';
  22.  
  23. // Application level variables
  24. //
  25. // Purpose
  26. // Variables that are persisent across all user sessions,
  27. // not just a specific user ala normal sessions.
  28. //
  29. // Usage:
  30. //
  31. // Example 1:
  32. //
  33. // $app  =& new Cache_Application();
  34. // $_APP =& $app->getData();
  35. //
  36. // In this case the $_APP variable is akin to the $_SESSION variable.
  37. // If you add/remove stuff, it will be reflected in the next request
  38. // (of any user).
  39. //
  40. // Example 2:
  41. //
  42. // $foo = 'Some data';
  43. // $bar = 'Some other data';
  44. //
  45. // $app =& new Cache_Application();
  46. // $app->register('foo');
  47. // $app->register('bar', $bar); 
  48. //
  49. // $foo = 'Different data';
  50. //
  51. // In this case the variables are registered with the register() function.
  52. // This is akin to session_register().
  53. //
  54. // As with session_register(), the contents of the variable at the *end* of the
  55. // request is registered and not at the point of registration. Therefore in this
  56. // example, for the $foo variable, the string 'Different data' is stored and not 
  57. // 'Some data'. The exception to this rule is if you use the second argument to
  58. // register() as in the second call to it above. This will cause the data supplied
  59. // in the second argument to be stored and not the contents at the end of the request.
  60. //
  61. // Note: If you use this method with register_globals turned on, the variables will be
  62. //       automatically globalled upon startup, (ie. when you create the object).
  63. //
  64. // Note: If you register a variable that is not set when the script finishes, it will
  65. //       registered as NULL.
  66. //
  67. //
  68. // *** You are strongly recommended to use only one method of the two above. ***
  69. //
  70. // (In fact if you use the register() function with register_globals Off, you have to
  71. //  use the $_APP method to get at the data).
  72.  
  73. class Cache_Application extends Cache {
  74.  
  75.     var $data;
  76.     var $id;
  77.     var $group;
  78.     var $registered_vars;
  79.  
  80.     /**
  81.     * Constructor
  82.     *
  83.     * @param    string  Name of container class
  84.     * @param    array   Array with container class options
  85.     */
  86.     function Cache_Application($container = 'file', $container_options = array('cache_dir' => '/tmp/', 'filename_prefix' => 'cache_'), $id = 'application_var', $group = 'application_cache')
  87.     {
  88.         $this->id    = $id;
  89.         $this->group = $group;
  90.         $this->registered_vars = array();
  91.  
  92.         $this->Cache($container, $container_options);
  93.         $this->data = $this->isCached($this->id, $this->group) ? unserialize($this->get($this->id, $this->group)) : array();
  94.  
  95.         // If register_globals on, global all registered variables
  96.         if (ini_get('register_globals') AND is_array($this->data)) {
  97.             foreach ($this->data as $key => $value) {
  98.                 global $$key;
  99.                 $$key = $value;
  100.             }
  101.         }
  102.     }
  103.  
  104.     /**
  105.     * Destructor
  106.     *
  107.     * Gets values of all registered variables and stores them. Then calls save() to
  108.     * write data away.
  109.     */
  110.     function _Cache_Application()
  111.     {
  112.         // Get contents of all registered variables
  113.         if (is_array($this->registered_vars) AND !empty($this->registered_vars)) {
  114.             foreach ($this->registered_vars as $varname) {
  115.                 global $$varname;
  116.                 $this->data[$varname] = $$varname;
  117.             }
  118.         }
  119.  
  120.         // Save the data
  121.         $this->save($this->id, serialize($this->data), 0, $this->group);
  122.     }
  123.  
  124.     /**
  125.     * register()
  126.     *
  127.     * Registers a variable to be stored.
  128.     *
  129.     * @param    string  Name of variable to register
  130.     * @param    mixed   Optional data to store
  131.     */
  132.     function register($varname, $data = null)
  133.     {
  134.         if (isset($data)) {
  135.             $this->data[$varname] = $data;
  136.         } else {
  137.             $this->registered_vars[] = $varname;
  138.         }
  139.     }
  140.  
  141.     /**
  142.     * unregister()
  143.     *
  144.     * Unregisters a variable from being stored.
  145.     *
  146.     * @param    string  Name of variable to unregister
  147.     */
  148.     function unregister($varname)
  149.     {
  150.         if (isset($this->data[$varname])) {
  151.             unset($this->data[$varname]);
  152.         }
  153.     }
  154.  
  155.     /**
  156.     * clear()
  157.     *
  158.     * Removes all stored data
  159.     */
  160.     function clear()
  161.     {
  162.         $this->data = array();
  163.     }
  164.  
  165.     /**
  166.     * getData()
  167.     *
  168.     * Use this to get a reference to the data to manipulate
  169.     * in calling script. Eg. $_APP =& $obj->getData();
  170.     *
  171.     * @return mixed   A reference to the data
  172.     */
  173.     function &getData()
  174.     {
  175.         return $this->data;
  176.     }
  177. }
  178. ?>